﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ESF.General;
using System.Collections.ObjectModel;
using System.Diagnostics;
using ESF.General.Text;
using System.IO;
using ESF.SFT.FileUploader.SharedResources;
using ESF.WinForms;
using ESF.WinForms.Elements;

#endregion


namespace ESF.SFT.FileUploader
{
	/// <summary>
	/// Interaction logic for ValidationLogViewer.xaml
	/// </summary>
	public partial class ValidationLogViewer : BaseWPFWindow
	{
		#region Members

		private FileData _FileData;

		#endregion


		#region Properties

		public FileData FileData
		{
			set
			{
				if (value == null)
				{
					Hide ();

					return;
				}

				_FileData = value;

				lblFileName.Text = value.FileInfo.FullName;

				lblFileType.Content = (value.ValidatorSet == null ? "No identificado" : value.ValidatorSet.Name);
				lblFileType.ToolTip = string.Format ("Separador de Decimales: ({0}), Cultura {1}", value.CultureInfo.NumberFormat.CurrencyDecimalSeparator, value.CultureInfo.Name);
				
				grvErrors.SelectedIndex = -1;
				
				if (DataHelper.IsEmpty(value.Errors))
				{
					lblFileStatus.Text = "El archivo NO contiene errores. Tiene su estructura correcta.";
				}
				else
				{
					lblFileStatus.Text = string.Format ("El archivo contiene {0} errores, su estructura o datos son inválidos", value.Errors.Count ());
				}

				grvErrors.ItemsSource = new ObservableCollection<GenericValidationException>(value.Errors);

				lblOnlyExtension.Text = value.OnlyExtension;

				lblOnlyName.Text = value.OnlyName;
			}
			get
			{
				return _FileData;
			}
		}

		#endregion


		#region Constructor
		
		public ValidationLogViewer() : base ()
		{
			InitializeComponent();

			IncludeVersionInTitle();

			Icon = WinFormsHelper.ToBitmapSource(SharedImages.Upload_64x64);
		}

		#endregion


		#region Window Events

		private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			e.Cancel = true;

			Hide ();
		}

		#endregion


		#region TextBlock Events

		private void lblFileName_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			if (File.Exists (lblFileName.Text))
			{
				ProcessHelper.StartProcess (lblFileName.Text);
			}
			else
			{
				MessageBox.Show(this, "El archivo seleccionado ya no esta en la ubicación indicada", "Archivo inexistente", MessageBoxButton.OK, MessageBoxImage.Exclamation);
			}
		}

		#endregion
	}
}
